package smecta.client.print.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import smecta.client.print.common.constants.InitialSetupConstant;
import smecta.client.print.domain.ScpSysSettings;
import smecta.client.print.service.ScpSysSettingsService;
import smecta.client.print.util.AlertUtil;

import java.io.File;
import java.util.Objects;
import java.util.Properties;

@Slf4j
@Component
public class InitialSetupController {

    @Autowired
    private ScpSysSettingsService scpSysSettingsService;

    @FXML
    private TextField companyName, dateChangeTime, shortcutKey, qrSize, edgeSize, da_tuopanxishu, da_xiangdanzhangshu, da_tongzhong, da_tongshu, da_xiangzhong, da_peizhongwucha, da_chengzhongf, da_chengzhongf1, da_tongzhongf, da_tongzhongf1, xiao_tuopanxishu, xiao_xiangdanzhangshu, xiao_tongzhong, xiao_tongshu, xiao_xiangzhong, xiao_peizhongwucha, xiao_chengzhongf, xiao_chengzhongf1, xiao_tongzhongf, xiao_tongzhongf1, chesi_tuopanxishu, chesi_xiangdanzhangshu, chesi_tongzhong, chesi_tongshu, chesi_xiangzhong, chesi_peizhongwucha, chesi_chengzhongf, chesi_chengzhongf1, chesi_tongzhongf, chesi_tongzhongf1, maodanPath;
    @FXML
    private CheckBox fullTrayPrint, fullTrayBarcodePrint, differentDateContinuation, sameDayContinuation, autoRead, enableQrPrint, da_defaultScheme, xiao_defaultScheme, chesi_defaultScheme;
    @FXML
    private ComboBox<String> encodingType, da_maozhongDecimals, da_shujushiji, da_jingzhongDecimals, xiao_maozhongDecimals, xiao_shujushiji, xiao_jingzhongDecimals, chesi_maozhongDecimals, chesi_shujushiji, chesi_jingzhongDecimals;

    private Properties properties = new Properties();

    @FXML
    private Button saveButton;
    @FXML
    private Button closeButton;

    @FXML
    public void initialize() {
        loadSettings();
        initializeShortcutKeyListener();
    }

    private Properties loadFromDB() {
        ScpSysSettings setting = scpSysSettingsService.findByKey(InitialSetupConstant.DB_DICT_KEY);
        if (Objects.isNull(setting)) {
            return properties;
        }
        Properties currentProp = JSONObject.parseObject(setting.getSettingValue(), Properties.class);
        properties = currentProp;
        return properties;
    }

    private void initializeShortcutKeyListener() {
        shortcutKey.setOnKeyPressed(event -> {
            shortcutKey.setText(event.getCode().getName());
            event.consume();
        });
    }

    @FXML
    private void handleSave() {
        properties.setProperty("companyName", companyName.getText());
        properties.setProperty("dateChangeTime", dateChangeTime.getText());
        properties.setProperty("fullTrayPrint", String.valueOf(fullTrayPrint.isSelected()));
        properties.setProperty("fullTrayBarcodePrint", String.valueOf(fullTrayBarcodePrint.isSelected()));
        properties.setProperty("differentDateContinuation", String.valueOf(differentDateContinuation.isSelected()));
        properties.setProperty("sameDayContinuation", String.valueOf(sameDayContinuation.isSelected()));
        properties.setProperty("autoRead", String.valueOf(autoRead.isSelected()));
        properties.setProperty("shortcutKey", shortcutKey.getText());
        properties.setProperty("enableQrPrint", String.valueOf(enableQrPrint.isSelected()));
        properties.setProperty("qrSize", qrSize.getText());
        properties.setProperty("edgeSize", edgeSize.getText());
        properties.setProperty("encodingType", encodingType.getValue());

        saveSectionSettings("da", da_tuopanxishu, da_xiangdanzhangshu, da_tongzhong, da_tongshu, da_xiangzhong, da_maozhongDecimals, da_shujushiji, da_jingzhongDecimals, da_peizhongwucha, da_chengzhongf, da_chengzhongf1, da_tongzhongf, da_tongzhongf1, da_defaultScheme);
        saveSectionSettings("xiao", xiao_tuopanxishu, xiao_xiangdanzhangshu, xiao_tongzhong, xiao_tongshu, xiao_xiangzhong, xiao_maozhongDecimals, xiao_shujushiji, xiao_jingzhongDecimals, xiao_peizhongwucha, xiao_chengzhongf, xiao_chengzhongf1, xiao_tongzhongf, xiao_tongzhongf1, xiao_defaultScheme);
        saveSectionSettings("chesi", chesi_tuopanxishu, chesi_xiangdanzhangshu, chesi_tongzhong, chesi_tongshu, chesi_xiangzhong, chesi_maozhongDecimals, chesi_shujushiji, chesi_jingzhongDecimals, chesi_peizhongwucha, chesi_chengzhongf, chesi_chengzhongf1, chesi_tongzhongf, chesi_tongzhongf1, chesi_defaultScheme);

        properties.setProperty("maodanPath", maodanPath.getText());

        scpSysSettingsService.updateKeyOrInit(InitialSetupConstant.DB_DICT_KEY, JSON.toJSONString(properties));
        AlertUtil.infoFinishAutoClose("保存成功", 3000L);

        Stage stage = (Stage) saveButton.getScene().getWindow();
        stage.close();
    }

    @FXML
    private void handleClose() {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }

    @FXML
    private void handleChoosePath() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        File selectedDirectory = directoryChooser.showDialog(companyName.getScene().getWindow());
        if (selectedDirectory != null) {
            maodanPath.setText(selectedDirectory.getAbsolutePath());
        }
    }

    private void loadSettings() {
        loadFromDB();
        companyName.setText(properties.getProperty("companyName", ""));
        dateChangeTime.setText(properties.getProperty("dateChangeTime", ""));
        fullTrayPrint.setSelected(Boolean.parseBoolean(properties.getProperty("fullTrayPrint", "false")));
        fullTrayBarcodePrint.setSelected(Boolean.parseBoolean(properties.getProperty("fullTrayBarcodePrint", "false")));
        differentDateContinuation.setSelected(Boolean.parseBoolean(properties.getProperty("differentDateContinuation", "false")));
        sameDayContinuation.setSelected(Boolean.parseBoolean(properties.getProperty("sameDayContinuation", "false")));
        autoRead.setSelected(Boolean.parseBoolean(properties.getProperty("autoRead", "false")));
        shortcutKey.setText(properties.getProperty("shortcutKey", ""));
        enableQrPrint.setSelected(Boolean.parseBoolean(properties.getProperty("enableQrPrint", "false")));
        qrSize.setText(properties.getProperty("qrSize", ""));
        edgeSize.setText(properties.getProperty("edgeSize", ""));
        encodingType.setValue(properties.getProperty("encodingType", ""));

        loadSectionSettings("da", da_tuopanxishu, da_xiangdanzhangshu, da_tongzhong, da_tongshu, da_xiangzhong, da_maozhongDecimals, da_shujushiji, da_jingzhongDecimals, da_peizhongwucha, da_chengzhongf, da_chengzhongf1, da_tongzhongf, da_tongzhongf1, da_defaultScheme);
        loadSectionSettings("xiao", xiao_tuopanxishu, xiao_xiangdanzhangshu, xiao_tongzhong, xiao_tongshu, xiao_xiangzhong, xiao_maozhongDecimals, xiao_shujushiji, xiao_jingzhongDecimals, xiao_peizhongwucha, xiao_chengzhongf, xiao_chengzhongf1, xiao_tongzhongf, xiao_tongzhongf1, xiao_defaultScheme);
        loadSectionSettings("chesi", chesi_tuopanxishu, chesi_xiangdanzhangshu, chesi_tongzhong, chesi_tongshu, chesi_xiangzhong, chesi_maozhongDecimals, chesi_shujushiji, chesi_jingzhongDecimals, chesi_peizhongwucha, chesi_chengzhongf, chesi_chengzhongf1, chesi_tongzhongf, chesi_tongzhongf1, chesi_defaultScheme);

        maodanPath.setText(properties.getProperty("maodanPath", ""));
    }

    private void saveSectionSettings(String prefix, TextField tuopanxishu, TextField xiangdanzhangshu, TextField tongzhong, TextField tongshu, TextField xiangzhong, ComboBox<String> maozhongDecimals, ComboBox<String> shujushiji, ComboBox<String> jingzhongDecimals, TextField peizhongwucha, TextField chengzhongf, TextField chengzhongf1, TextField tongzhongf, TextField tongzhongf1, CheckBox defaultScheme) {
        properties.setProperty(prefix + "_tuopanxishu", tuopanxishu.getText());
        properties.setProperty(prefix + "_xiangdanzhangshu", xiangdanzhangshu.getText());
        properties.setProperty(prefix + "_tongzhong", tongzhong.getText());
        properties.setProperty(prefix + "_tongshu", tongshu.getText());
        properties.setProperty(prefix + "_xiangzhong", xiangzhong.getText());
        properties.setProperty(prefix + "_maozhongDecimals", maozhongDecimals.getValue());
        properties.setProperty(prefix + "_shujushiji", shujushiji.getValue());
        properties.setProperty(prefix + "_jingzhongDecimals", jingzhongDecimals.getValue());
        properties.setProperty(prefix + "_peizhongwucha", peizhongwucha.getText());
        properties.setProperty(prefix + "_chengzhongf", chengzhongf.getText());
        properties.setProperty(prefix + "_chengzhongf1", chengzhongf1.getText());
        properties.setProperty(prefix + "_tongzhongf", tongzhongf.getText());
        properties.setProperty(prefix + "_tongzhongf1", tongzhongf1.getText());
        properties.setProperty(prefix + "_defaultScheme", String.valueOf(defaultScheme.isSelected()));
    }

    private void loadSectionSettings(String prefix, TextField tuopanxishu, TextField xiangdanzhangshu, TextField tongzhong, TextField tongshu, TextField xiangzhong, ComboBox<String> maozhongDecimals, ComboBox<String> shujushiji, ComboBox<String> jingzhongDecimals, TextField peizhongwucha, TextField chengzhongf, TextField chengzhongf1, TextField tongzhongf, TextField tongzhongf1, CheckBox defaultScheme) {
        tuopanxishu.setText(properties.getProperty(prefix + "_tuopanxishu", ""));
        xiangdanzhangshu.setText(properties.getProperty(prefix + "_xiangdanzhangshu", ""));
        tongzhong.setText(properties.getProperty(prefix + "_tongzhong", ""));
        tongshu.setText(properties.getProperty(prefix + "_tongshu", ""));
        xiangzhong.setText(properties.getProperty(prefix + "_xiangzhong", ""));
        maozhongDecimals.setValue(properties.getProperty(prefix + "_maozhongDecimals", ""));
        shujushiji.setValue(properties.getProperty(prefix + "_shujushiji", ""));
        jingzhongDecimals.setValue(properties.getProperty(prefix + "_jingzhongDecimals", ""));
        peizhongwucha.setText(properties.getProperty(prefix + "_peizhongwucha", ""));
        chengzhongf.setText(properties.getProperty(prefix + "_chengzhongf", ""));
        chengzhongf1.setText(properties.getProperty(prefix + "_chengzhongf1", ""));
        tongzhongf.setText(properties.getProperty(prefix + "_tongzhongf", ""));
        tongzhongf1.setText(properties.getProperty(prefix + "_tongzhongf1", ""));
        defaultScheme.setSelected(Boolean.parseBoolean(properties.getProperty(prefix + "_defaultScheme", "false")));
    }
}
